<?php

declare(strict_types=1);

namespace app\customer\controller;

use app\customer\controller\Base;
use think\Request;

/**
 * 联系人控制器
 */
class CustomerContact extends Base
{

    protected $request;
    protected $service;
    protected $validate;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\customer\service\CustomerContact();
        $this->validate = new \app\customer\validate\CustomerContact();
    }

    /**
     * @describe 获取联系人列表
     * @author: Bobo
     * @date: 2024-06-05 03:04:33 
     * @param [] type
     * @return Renderable
     */
    public function index()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnJsonData([], 500, $this->validate->getError());
        }

        $pageIndex  = isset($params['pageIndex'])   ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])    ? intval($params['pageSize']) : 10;
        $cusid      = isset($params['cusid'])       ? intval($params['cusid']) : null;
        $gender     = isset($params['gender'])       ? intval($params['gender']) : null;
        $createTime = isset($params['create_time'])  ? trim($params['create_time']) : null;
        $updateTime = isset($params['update_time'])  ? trim($params['update_time']) : null;


        $where = ['is_del' => 0];
        if ($cusid > 0) {
            $where['cusid'] = $cusid;
        }
        if ($gender > -1) {
            $where['gender'] = $gender;
        }
        if ($createTime) {
            $where['create_time'] = $createTime;
        }
        if ($updateTime) {
            $where['update_time'] = $updateTime;
        }

        $result = $this->service->getList($where, $pageIndex, $pageSize);
        return $this->returnJsonData($result);
    }
    /**
     * @describe 获取联系人详情
     * @author: Bobo
     * @date: 2024-06-05 03:07:37 
     * @param [int] $id 联系人ID @required
     * @return Renderable
     */
    public function show()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('show')->check($params)) {
            return $this->returnJsonData([], 422, $this->validate->getError());
        }
        $result = $this->service->getOneById($params['id']);
        return $this->returnJsonData($result);
    }

    /**
     * @describe 创建联系人
     * @author: Bobo
     * @date: 2024-06-05 03:09:17 
     * @param [array] $data 创建联系人的数据 @required
     * @return Renderable
     */
    public function create()
    {
        $data = $this->request->params;
        $data['creator'] = 1;

        if (!$this->validate->scene('create')->check($data)) {
            return $this->returnJsonData([], 422, $this->validate->getError());
        }

        $result = $this->service->create($data);

        return $this->returnJsonData($result);
    }

    /**
     * 更新联系人
     * @describe 更新联系人
     * @author: Bobo
     * @date: 2024-06-05 03:11:17 
     * @param [int] $id 联系人ID @required
     * @param [array] $data 更新联系人的数据 @required
     * @return Renderable
     */
    public function update()
    {

        $save = $this->request->params;

        if (!$this->validate->scene('update')->check($save)) {
            return $this->returnJsonData([], 500, $this->validate->getError());
        }
        $result = $this->service->update($save);

        return $this->returnJsonData($result);
    }

    /**
     * 删除联系人
     * @param int $id 联系人ID
     */
    /**
     * @describe 删除联系人
     * @author: Bobo
     * @date: 2024-06-05 03:13:56 
     * @param [int] $id 联系人ID @required
     * @return Renderable
     */
    public function delete()
    {
        $ids = $this->request->params['ids'];
        if (!$this->validate->scene('delete')->check($ids)) {
            return $this->returnJsonData([], 500, $this->validate->getError());
        }

        $result = $this->service->batchLogicalDelete($ids);
        return $this->returnJsonData($result);
    }
}
